package com.xuecheng.content.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xuecheng.api.content.model.dto.CourseBaseDTO;
import com.xuecheng.api.content.model.qo.QueryCourseBaseModel;
import com.xuecheng.common.domain.page.PageRequestParams;
import com.xuecheng.common.domain.page.PageVO;
import com.xuecheng.common.util.StringUtil;
import com.xuecheng.content.convert.CourseBaseConvert;
import com.xuecheng.content.entity.CourseBase;
import com.xuecheng.content.mapper.CourseBaseMapper;
import com.xuecheng.content.service.CourseBaseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 课程基本信息 服务实现类
 * </p>
 *
 * @author itcast
 */
@Slf4j
@Service
public class CourseBaseServiceImpl extends ServiceImpl<CourseBaseMapper, CourseBase> implements CourseBaseService {

    /*
     * 步骤分析：
     *  1.是否需要开启事务  查询不需要开启事务
     *  2.判断关键数据
     *       分页数据
     *       查询条件
     *  3.构建mp分页对象
     *  4.构建查询条件对象LambdaQueryWrapper
     *  5.查询数据
     *  6.获得数据并封装返回结果
     *       封装PageVo数据
     * */
    @Override
    public PageVO queryCourseBaseList(Long companyId,PageRequestParams params, QueryCourseBaseModel model) {
        //1.是否需要开启事务  查询不需要开启事务
        //2.判断关键数据
        //    分页数据
        if (params.getPageNo() < 1) {
            params.setPageNo(PageRequestParams.DEFAULT_PAGE_NUM);
        }

        //    查询条件
        if (params.getPageSize() < 1) {
            params.setPageSize(PageRequestParams.DEFAULT_PAGE_SIZE);
        }

        //3.构建mp分页对象
        Page<CourseBase> page = new Page<>(params.getPageNo(), params.getPageSize());

        //4.构建查询条件对象LambdaQueryWrapper
        LambdaQueryWrapper<CourseBase> queryWrapper = new LambdaQueryWrapper<>(CourseBase.class);

//        if (StringUtil.isNotBlank(model.getAuditStatus())) {
//            queryWrapper.eq(CourseBase::getAuditStatus, model.getAuditStatus());
//        }
//
//        if (StringUtil.isNotBlank(model.getCourseName())) {
//            queryWrapper.like(CourseBase::getName, model.getCourseName());
//        }
        //简化
        queryWrapper.eq(StringUtil.isNotBlank(model.getAuditStatus()),CourseBase::getAuditStatus, model.getAuditStatus());
        queryWrapper.like(StringUtil.isNotBlank(model.getCourseName()),CourseBase::getName, model.getCourseName());
        //5.查询数据
        this.page(page, queryWrapper);

        //6.获得数据并封装返回结果
        //获取查询结果的list集合
        List<CourseBase> list = page.getRecords();
        //获取总记录数
        long total = page.getTotal();

        //todo:将po数据转换为dto
        List<CourseBaseDTO> dtos = Collections.emptyList();
        if (!CollectionUtils.isEmpty(list)) {
            dtos = CourseBaseConvert.INSTANCE.entitys2dtos(list);
        }

        //封装PageVo数据
        PageVO pageVO = new PageVO(dtos, total, params.getPageNo(), params.getPageSize());
        return pageVO;
    }
}
